# Include configuration
include src/lib/config.mk

# Compiler settings
CC := gcc
CXX := g++

# Default to native build
CROSS_COMPILE := 
HOST := 

# If cross compilation is enabled, set the toolchain
ifeq ($(CONFIG_CROSS_COMPILE),y)
	CROSS_COMPILE := $(CONFIG_CROSS_HOST)-
	CC := $(CROSS_COMPILE)gcc
	CXX := $(CROSS_COMPILE)g++
endif


# Default build type
BUILD_TYPE ?= debug
ENABLE_MQTT_TEST ?= n

# Cross compile settings
CROSS_COMPILE ?= n
CROSS_HOST ?= 

# Build type settings
BUILD_TYPE := $(CONFIG_BUILD_TYPE)
ifeq ($(CONFIG_BUILD_TYPE),release)
	BUILD_TYPE := release
endif

# Compiler flags
CFLAGS := -Wextra -Wall -std=gnu99 -fPIC
CXXFLAGS := -Wall -Wextra -std=c++11 -DENABLE_MQTT
INCLUDES := -Isrc/lib/com_inc -Isrc/lw_mqtt -Isrc/lw_log -Isrc/lib/EasyLogger/easylogger/inc -Isrc/lib/EasyLogger/easylogger/port

# Debug/Release specific flags
ifeq ($(BUILD_TYPE),debug)
	CFLAGS += -g -DDEBUG
	CXXFLAGS += -g -DDEBUG
else
	CFLAGS += -O2
	CXXFLAGS += -O2
endif

# Library paths
LIB_DIRS := -L$(BUILD_DIR) -Lsrc/lib/com_lib

# Link libraries
LIBS_MQTT := -lmqttc -lpthread
LIBS_LOG := -lpthread
LDFLAGS := $(LIB_DIRS) -llw_mqtt -llw_log $(LIBS_MQTT)

# MQTT-C library path
MQTT_C_DIR := src/lib/MQTT-C
MQTT_LIB := src/lib/com_lib/libmqttc.so

# EasyLogger sources
ELOG_DIR := src/lib/EasyLogger/easylogger
ELOG_SOURCES := $(wildcard $(ELOG_DIR)/src/*.c) $(ELOG_DIR)/port/elog_port.c
ELOG_OBJECTS := $(ELOG_SOURCES:.c=.o)

# Source files list
CPP_SOURCES_COMMON = src/lw_log/lw_log.cpp \
                     src/lw_mqtt/lw_mqtt.cpp \
                     src/lib/EasyLogger/easylogger/src/elog.cpp \
                     src/lib/EasyLogger/easylogger/src/elog_buf.cpp \
                     src/lib/EasyLogger/easylogger/src/elog_utils.cpp \
                     src/lib/MQTT-C/src/mqtt.cpp \
                     src/lib/MQTT-C/src/mqtt_pal.cpp

# Main application source files
CPP_SOURCES_MAIN = src/main.cpp

# Standalone MQTT test source files
CPP_SOURCES_MQTT_TEST = src/test_mqtt_standalone.cpp

# Combine source files based on configuration
CPP_SOURCES = $(CPP_SOURCES_COMMON)

ifeq ($(ENABLE_MQTT_TEST),y)
CPP_SOURCES += $(CPP_SOURCES_MQTT_TEST)
endif

# Object files
CPP_OBJECTS = $(CPP_SOURCES:.cpp=.o)
C_SOURCES := $(wildcard src/lib/MQTT-C/src/*.c) $(ELOG_SOURCES)
C_OBJECTS := $(C_SOURCES:.c=.o)

# Build directory
BUILD_DIR := build

# Targets
.PHONY: all mqtt lw_mqtt test_mqtt example_mqtt lw_log test_log example_log main clean help

all: libs lw_mqtt lw_log main

# Build all libraries
libs:
	@echo "Building all libraries..."
	@cd src/lib && ./build.sh $(if $(findstring y,$(CONFIG_CROSS_COMPILE)),--cross-compile --host $(CONFIG_CROSS_HOST),)

# Build MQTT-C library
mqtt:
	@echo "Building MQTT-C library..."
	@cd src/lib && ./build.sh $(if $(findstring y,$(CONFIG_CROSS_COMPILE)),--cross-compile --host $(CONFIG_CROSS_HOST),)

# Build C++ MQTT wrapper
lw_mqtt: mqtt
	@echo "Building C++ MQTT wrapper..."
	@mkdir -p $(BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -fPIC -shared -o $(BUILD_DIR)/liblw_mqtt.so src/lw_mqtt/lw_mqtt.cpp -Lsrc/lib/com_lib -lmqttc -lpthread

# Build C log library
lw_log: $(ELOG_OBJECTS)
	@echo "Building C log library..."
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(INCLUDES) -fPIC -shared -o $(BUILD_DIR)/liblw_log.so src/lw_log/lw_log.c $(ELOG_OBJECTS) $(LIBS_LOG)

# Build main program
main: libs $(CPP_OBJECTS)
	@echo "Building main program..."
	@mkdir -p $(BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(BUILD_DIR)/main $(CPP_SOURCES_MAIN) $(CPP_OBJECTS) -L$(BUILD_DIR) -Lsrc/lib/com_lib -llw_mqtt -llw_log -lmqttc -lpthread

# Standalone MQTT test target
mqtt_test: CPP_SOURCES = $(CPP_SOURCES_COMMON) $(CPP_SOURCES_MQTT_TEST)
mqtt_test: CFLAGS += -DENABLE_MQTT_TEST
mqtt_test: CXXFLAGS += -DENABLE_MQTT_TEST
mqtt_test: $(CPP_OBJECTS)
	@echo "Linking standalone MQTT test..."
	@mkdir -p $(BUILD_DIR)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(BUILD_DIR)/mqtt_test $(CPP_SOURCES_MQTT_TEST) $(CPP_OBJECTS) $(LDFLAGS)
	@echo "Standalone MQTT test built successfully!"

# Build example program for MQTT
example_mqtt: lw_mqtt
	@echo "Building example program..."
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(BUILD_DIR)/example_mqtt src/example_mqtt.cpp $(LIB_DIRS) -llw_mqtt $(LIBS_MQTT)

# Build test program for log
test_log: lw_log
	@echo "Building log test program..."
	$(CC) $(CFLAGS) $(INCLUDES) -o $(BUILD_DIR)/test_log src/test_log.c -L$(BUILD_DIR) -llw_log $(LIBS_LOG)

# Build example program for log
example_log: lw_log
	@echo "Building log example program..."
	$(CC) $(CFLAGS) $(INCLUDES) -o $(BUILD_DIR)/example_log src/example_log.c -L$(BUILD_DIR) -llw_log $(LIBS_LOG)

# Compile EasyLogger objects
$(ELOG_DIR)/src/%.o: $(ELOG_DIR)/src/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(ELOG_DIR)/port/%.o: $(ELOG_DIR)/port/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	rm -rf $(BUILD_DIR)
	@cd src/lib && ./build.sh --disable-mqtt
	rm -f $(ELOG_OBJECTS)

# Show help
help:
	@echo "Usage: make [OPTIONS] [TARGET]"
	@echo ""
	@echo "OPTIONS:"
	@echo "  CONFIG_BUILD_TYPE=debug|release    Set build type (default: debug)"
	@echo "  CONFIG_CROSS_COMPILE=y|n           Enable cross compile (default: n)"
	@echo "  CONFIG_CROSS_HOST=host             Set cross compile host (default: empty)"
	@echo "  CONFIG_ENABLE_MQTT_TEST=y|n        Enable standalone MQTT test (default: n)"
	@echo ""
	@echo "TARGETS:"
	@echo "  all             Build main application (default)"
	@echo "  clean           Clean build directory"
	@echo "  mqtt_test       Build standalone MQTT test application"
	@echo "  help            Show this help message"
	@echo ""
	@echo "Examples:"
	@echo "  make                             # Build main app in debug mode"
	@echo "  make CONFIG_BUILD_TYPE=release   # Build in release mode"
	@echo "  make mqtt_test                   # Build standalone MQTT test"
	@echo "  make CONFIG_ENABLE_MQTT_TEST=y   # Enable MQTT test in main build"